<?php

/**
 * This is the model class for table "ciclo".
 *
 * The followings are the available columns in table 'ciclo':
 * @property integer $id
 * @property integer $anio
 * @property integer $id_periodo
 * @property string $fecha_inicio
 * @property string $fecha_fin
 * @property string $usuario_ing
 * @property string $creado
 * @property string $usuario_act
 * @property string $fecha_act
 *
 * The followings are the available model relations:
 * @property Periodo $idPeriodo
 * @property CursoCiclo[] $cursoCiclos
 */
class Ciclo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Ciclo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ciclo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('anio, id_periodo, fecha_inicio, fecha_fin', 'required'),
			array('anio, id_periodo', 'numerical', 'integerOnly'=>true),
			array('usuario_ing, usuario_act', 'length', 'max'=>45),
			array('fecha_act', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, anio, id_periodo, fecha_inicio, fecha_fin, usuario_ing, creado, usuario_act, fecha_act', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'periodo' => array(self::BELONGS_TO, 'Periodo', 'id_periodo'),
			'cursoCiclos' => array(self::HAS_MANY, 'CursoCiclo', 'id_ciclo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'anio' => 'Anio',
			'id_periodo' => 'Id Periodo',
			'fecha_inicio' => 'Fecha Inicio',
			'fecha_fin' => 'Fecha Fin',
			'usuario_ing' => 'Usuario Ing',
			'creado' => 'Creado',
			'usuario_act' => 'Usuario Act',
			'fecha_act' => 'Fecha Act',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('anio',$this->anio);
		$criteria->compare('id_periodo',$this->id_periodo);
		$criteria->compare('fecha_inicio',$this->fecha_inicio,true);
		$criteria->compare('fecha_fin',$this->fecha_fin,true);
		$criteria->compare('usuario_ing',$this->usuario_ing,true);
		$criteria->compare('creado',$this->creado,true);
		$criteria->compare('usuario_act',$this->usuario_act,true);
		$criteria->compare('fecha_act',$this->fecha_act,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave() {	
		if ($this->isNewRecord){
			$this->creado = new CDbExpression('NOW()');
		}	
		return parent::beforeSave();
	}
	
	public function obtenerUltimoCiclo(){
		return Ciclo::model()->find(array('order'=>'creado desc','limit'=>1));
	}
}